/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>RoomStyleServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年9月18日 下午7:46:47<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HAddressMapper;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.dao.HotelRoomMapper;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HotelRoom;
import com.flea.hclv.service.HotelRoomService;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description
 * @createTime 2018年9月18日 下午7:46:47
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Service("hotelRoomStyleService")
public class HotelRoomServiceImpl implements HotelRoomService {
	@Autowired
	private HotelRoomMapper hotelRoomMapper;
	@Autowired
	private HResourceRelationMapper hResourceRelationMapper;
	@Autowired
	private HAddressMapper addressMapper;

	@Override
	public boolean saveOrUpdate(HotelRoom entity,
			List<HResourceRelation> filesRelation, String userId) {
		String id = entity.getId();
		entity.setModifyUser(userId);
		int result = 0;
		if (StringUtil.isEmpty(id)) {
			id = FleaStringUtils.getGenerateUUID();
			entity.setId(id);
			entity.setCreateUser(userId);
	/*		OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
			String deptId = HConstant.HCLV_ADMIN_ADMIN;

			if (null != dept) {

				deptId = dept.getDeptUuid();
			}
			entity.setUuid(deptId);*/

			result = hotelRoomMapper.insertSelective(entity);

		} else {
			result = hotelRoomMapper.updateByPrimaryKeySelective(entity);

		}
		int fileSize = null==filesRelation?0:filesRelation.size();
		if (result > 0 && fileSize > 0) {
			int resourceCount = 0;
			
			for (HResourceRelation hResourceRelation : filesRelation) {
				String hResourceRelationId = hResourceRelation.getId();
				hResourceRelation.setModifyUser(userId);
				if(StringUtil.isEmpty(hResourceRelationId)){
					hResourceRelationId = FleaStringUtils.getGenerateUUID();
					hResourceRelation.setId(hResourceRelationId);
					hResourceRelation.setCreateUser(userId);
					hResourceRelation.setUuid(id);
					resourceCount +=	hResourceRelationMapper.insertSelective(hResourceRelation);
				}else{
					//只有删除和添加，并没有修改
					resourceCount += hResourceRelationMapper.updateByPrimaryKeySelective(hResourceRelation);
				}
			}
			return resourceCount==fileSize && resourceCount >0;
		}else{
			return result > 0;
		}

		
	}

	@Override
	public FleaPage<HotelRoom> findForGird(String queryName, String queryLevel,
			String queryProvince, String queryCity, String queryZone,String queryStyleType,
			int curPage, int pageSize, String userUuid) {
		OrgDept dept = FleaOrgUtils.getCurrentUserDept(userUuid);
		String deptId = dept == null ? HConstant.HCLV_ADMIN_ADMIN : dept
				.getDeptUuid();
    List<HAddress> address = null;
		if(!StringUtil.isEmpty(queryProvince)){
       address = addressMapper.selectByParams(queryProvince,queryCity,queryZone);
    }



		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		List<HotelRoom> pageData = hotelRoomMapper.selectPagingsByOrgParams(rowBounds, deptId,queryName,queryLevel,queryStyleType,address);
		FleaPage<HotelRoom> page = new FleaPage<>(pageData);
		return page;
	}

	@Override
	public boolean deleteByIds(String ids, String userId) {
		List<String>  idsArray = JSON.parseArray(ids, String.class);
		addressMapper.deleteByUuids(idsArray);
		int result = hotelRoomMapper.deleteByPrimaryKeys(idsArray);
		return result == idsArray.size() && result > 0;
	}

}
